﻿using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;
using Microsoft.VisualStudio.CommandBars;
using stdole;
namespace NUnitVSAddin
{
    /// <summary>The object for implementing an Add-in.</summary>
    /// <seealso class='IDTExtensibility2' />
    public class Connect : IDTExtensibility2, IDTCommandTarget
    {
        /// <summary>
        /// Gets the application object.
        /// </summary>
        /// <value>The application object.</value>
        public DTE2 ApplicationObject
        {
            get { return _applicationObject; }
        }

        /// <summary>
        /// Gets the add in instance.
        /// </summary>
        /// <value>The add in instance.</value>
        public AddIn AddInInstance
        {
            get { return _addInInstance; }
        }

        private VSMenu _vsMenu;
        /// <summary>
        /// Gets the menu.
        /// </summary>
        /// <value>The menu.</value>
        public VSMenu Menu
        {
            get { return _vsMenu; }
        }

        private NUnit _nunitit;

        /// <summary>
        /// Gets the Nunitit.
        /// </summary>
        /// <value>The Nunitit.</value>
        public NUnit NUnitit
        {
            get { return _nunitit; }
        }
        /// <summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
        public Connect()
        {
        }

        /// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
        /// <param term='application'>Root object of the host application.</param>
        /// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
        /// <param term='addInInst'>Object representing this Add-in.</param>
        /// <seealso class='IDTExtensibility2' />
        public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            _applicationObject = (DTE2)application;
            _addInInstance = (AddIn)addInInst;

            if (connectMode == ext_ConnectMode.ext_cm_Startup)
            {

                _nunitit = new NUnit(this);
                _vsMenu = new VSMenu(this, "NUnit Debug");

                VSMenuCommand debugConsoleCmd = _vsMenu.AddCommand("DebugConsole", "Debug NUnitConsole",
                                                                     "Debug with the NUnitConsole.");

                debugConsoleCmd.Execute += new EventHandler(debugConsoleCmd_Execute);
                debugConsoleCmd.Button.Style = MsoButtonStyle.msoButtonIcon;
                debugConsoleCmd.Button.Caption = "Run Using Console";
                debugConsoleCmd.Button.TooltipText = "Button 1";
                debugConsoleCmd.Button.Visible = true;
                debugConsoleCmd.Button.Enabled = true;

                VSMenuCommand debugGUICmd = _vsMenu.AddCommand("DebugGUI", "Debug NUnit GUI",
                                                               "Debug with the NUnit GUI.");

                debugGUICmd.Button.Caption = "Run Using NUnit GUI";
                debugGUICmd.Button.Style = MsoButtonStyle.msoButtonIcon;
                debugGUICmd.Button.Picture = ImageConverter.GetIPictureDispFromImage(Custom.PNG);
                debugGUICmd.Button.Visible = true;
                debugGUICmd.Button.Enabled = true;
                debugGUICmd.Execute += new EventHandler(debugGUICmd_Execute);


                VSMenuCommand settingscmd = _vsMenu.AddCommand("nunitsettings", "NUnit Settings",
                                                               "configure nunit.");
                settingscmd.Button.Caption = "NUnit Settings";
                settingscmd.Execute += new EventHandler(settingsCmd_Execute);
                settingscmd.Button.Style = MsoButtonStyle.msoButtonIcon;
                settingscmd.Button.TooltipText = "button 1";
                settingscmd.Button.Picture = ImageConverter.GetIPictureDispFromImage(Custom.PNG);
                settingscmd.Button.Visible = true;
                settingscmd.Button.Enabled = true;

                CommandBars cmdBars = (CommandBars)_applicationObject.CommandBars;

              //  CommandBar cmd = cmdBars["Context Menus"];
              //  CommandBarButton _button11 = (CommandBarButton)cmd.Controls.Add(MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

              //  _button11.Style = MsoButtonStyle.msoButtonIcon;
              //  _button11.Caption = "Context Menu";
              ////  _button11.Picture = ImageConverter.GetIPictureDispFromImage(Custom.NUnitPNG);
              //  _button11.TooltipText = "COntext MENU";
              //  _button11.Click += new _CommandBarButtonEvents_ClickEventHandler(OnButtonClick);
              //  _button11.Visible = true;


                CommandBar _myToolBar = cmdBars.Add("MyToolBar", MsoBarPosition.msoBarTop, Type.Missing, true);

                CommandBarButton _button1 = (CommandBarButton)_myToolBar.Controls.Add(MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, true);
                _button1.Style = MsoButtonStyle.msoButtonIcon;
                _button1.Caption = "Test With NUnit1";
                _button1.Picture = ImageConverter.GetIPictureDispFromImage(Custom.NUnitPNG);
                _button1.TooltipText = "Test With NUnit2";
                _button1.Click += new _CommandBarButtonEvents_ClickEventHandler(OnButtonClick);
                _myToolBar.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="CancelDefault"></param>
        private void OnButtonClick(CommandBarButton Ctrl, ref bool CancelDefault)
        {
          var  selectedItems = _applicationObject.ActiveDocument.Selection;
            this.debugGUICmd_Execute(Ctrl, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void debugGUICmd_Execute(object sender, EventArgs e)
        {
            _nunitit.LaunchNUnit(NUnitProgramType.GUI);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="caption"></param>
        /// <param name="message"></param>
        public void DisplayErrorMessage(string caption, string message)
        {
            WinWrapper w = new WinWrapper(this);
            MessageBox.Show(w, message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void debugConsoleCmd_Execute(object sender, EventArgs e)
        {
            _nunitit.LaunchNUnit(NUnitProgramType.Console);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void settingsCmd_Execute(object sender, EventArgs e)
        {
            DisplaySettings();
        }

        public void DisplaySettings()
        {
            NUnititSettings dlg = new NUnititSettings();
            WinWrapper w = new WinWrapper(this);
            dlg.ShowDialog(w);
        }

        /// <summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary>
        /// <param term='disconnectMode'>Describes how the Add-in is being unloaded.</param>
        /// <param term='custom'>Array of parameters that are host application specific.</param>
        /// <seealso class='IDTExtensibility2' />
        public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
        {
        }

        /// <summary>Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification when the collection of Add-ins has changed.</summary>
        /// <param term='custom'>Array of parameters that are host application specific.</param>
        /// <seealso class='IDTExtensibility2' />		
        public void OnAddInsUpdate(ref Array custom)
        {
        }

        /// <summary>Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading.</summary>
        /// <param term='custom'>Array of parameters that are host application specific.</param>
        /// <seealso class='IDTExtensibility2' />
        public void OnStartupComplete(ref Array custom)
        {
        }

        /// <summary>Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded.</summary>
        /// <param term='custom'>Array of parameters that are host application specific.</param>
        /// <seealso class='IDTExtensibility2' />
        public void OnBeginShutdown(ref Array custom)
        {
        }

        private DTE2 _applicationObject;
        private AddIn _addInInstance;

        /// <summary>
        /// Exec Command
        /// </summary>
        /// <param name="CmdName">Command Name</param>
        /// <param name="ExecuteOption">Execute Option</param>
        /// <param name="VariantIn">Variant In</param>
        /// <param name="VariantOut">Variant Out</param>
        /// <param name="Handled">Handled</param>
        public void Exec(string CmdName, vsCommandExecOption ExecuteOption, ref object VariantIn, ref object VariantOut, ref bool Handled)
        {
            _vsMenu.Exec(CmdName, ExecuteOption, ref VariantIn, ref VariantOut, ref Handled);
        }

        /// <summary>
        /// Query Status
        /// </summary>
        /// <param name="CmdName"></param>
        /// <param name="NeededText"></param>
        /// <param name="StatusOption"></param>
        /// <param name="CommandText"></param>
        public void QueryStatus(string CmdName, vsCommandStatusTextWanted NeededText, ref vsCommandStatus StatusOption, ref object CommandText)
        {
            if (NeededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
            {
                //if(commandName == "NUnitit.Connect.NUnitit")
                //{
                StatusOption = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                return;
                //}
            }
        }
    }
}